package com.edu.exam.enums.grading;

import com.edu.exam.enums.core.BaseEnum;

/**
 * 任务优先级枚举
 *
 * <p>定义评分系统中任务优先级，用于区分任务的紧急程度和重要性。</p>
 *
 * @author System
 * @version 1.0.0
 * @since 2025-11-17
 */
public enum TaskPriority implements BaseEnum {

    /**
     * 最低优先级
     */
    LOWEST("LOWEST", "最低优先级"),

    /**
     * 低优先级
     */
    LOW("LOW", "低优先级"),

    /**
     * 低于正常
     */
    BELOW_NORMAL("BELOW_NORMAL", "低于正常"),

    /**
     * 正常
     */
    NORMAL("NORMAL", "正常"),

    /**
     * 高于正常
     */
    ABOVE_NORMAL("ABOVE_NORMAL", "高于正常"),

    /**
     * 高优先级
     */
    HIGH("HIGH", "高优先级"),

    /**
     * 最高优先级
     */
    HIGHEST("HIGHEST", "最高优先级"),

    /**
     * 紧急
     */
    URGENT("URGENT", "紧急"),

    /**
     * 立即执行
     */
    IMMEDIATE("IMMEDIATE", "立即执行"),

    /**
     * 关键
     */
    CRITICAL("CRITICAL", "关键"),

    /**
     * 阻塞性
     */
    BLOCKING("BLOCKING", "阻塞性");

    private final String code;
    private final String description;

    TaskPriority(String code, String description) {
        this.code = code;
        this.description = description;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getDescription() {
        return description;
    }

    /**
     * 获取优先级数值（用于排序和比较）
     *
     * @return 优先级数值，值越大优先级越高
     */
    public int getPriorityValue() {
        switch (this) {
            case LOWEST: return 0;
            case LOW: return 1;
            case BELOW_NORMAL: return 2;
            case NORMAL: return 3;
            case ABOVE_NORMAL: return 4;
            case HIGH: return 5;
            case HIGHEST: return 6;
            case URGENT: return 7;
            case IMMEDIATE: return 8;
            case CRITICAL: return 9;
            case BLOCKING: return 10;
            default: return 3;
        }
    }

    /**
     * 判断是否为高优先级
     *
     * @return 是否为高优先级及以上
     */
    public boolean isHighPriority() {
        return this == HIGH || this == HIGHEST || this == URGENT || 
               this == IMMEDIATE || this == CRITICAL || this == BLOCKING;
    }

    /**
     * 判断是否为低优先级
     *
     * @return 是否为低优先级及以下
     */
    public boolean isLowPriority() {
        return this == LOWEST || this == LOW;
    }

    /**
     * 判断是否为正常优先级
     *
     * @return 是否为正常优先级
     */
    public boolean isNormalPriority() {
        return this == NORMAL || this == ABOVE_NORMAL || this == BELOW_NORMAL;
    }

    /**
     * 根据编码获取对应的枚举
     *
     * @param code 编码
     * @return 对应的枚举
     */
    public static TaskPriority fromCode(String code) {
        for (TaskPriority priority : values()) {
            if (priority.getCode().equals(code)) {
                return priority;
            }
        }
        throw new IllegalArgumentException("Unknown task priority code: " + code);
    }

    /**
     * 根据优先级数值获取对应的枚举
     *
     * @param priorityValue 优先级数值
     * @return 对应的枚举
     */
    public static TaskPriority fromPriorityValue(int priorityValue) {
        for (TaskPriority priority : values()) {
            if (priority.getPriorityValue() == priorityValue) {
                return priority;
            }
        }
        return NORMAL;
    }

    @Override
    public String toString() {
        return this.code + "(" + this.description + ")";
    }
}
